package top.shen33.yang.services.impl;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;
import top.shen33.yang.model.entity.SysField;
import top.shen33.yang.mapper.SysFieldMapper;
import top.shen33.yang.model.entity.SysFieldDetail;
import top.shen33.yang.services.SysFieldService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 字段 服务实现类
 * </p>
 *
 * @author oreilly
 * @since 2018-09-17
 */
@Service
public class SysFieldServiceImpl extends ServiceImpl<SysFieldMapper, SysField>implements SysFieldService {

    @Override
    public Class clazz() {
        return SysField.class;
    }

    @Override
    @Transactional
    public void doAdd(SysField itemSysField) {

        itemSysField.insertOrUpdate();

        updateFieldDetail(itemSysField);


    }

    private void updateFieldDetail(SysField itemSysField) {
        Integer fieldSid = itemSysField.getSid();
        //删除原来的
        new SysFieldDetail().delete(new QueryWrapper<SysFieldDetail>().eq("field_Id",fieldSid));

        //每次都新增
        List<SysFieldDetail> detailList = itemSysField.getDetailList();
        if(detailList != null && detailList.size() > 0){
            for (SysFieldDetail detail : detailList) {
                detail.setFieldId(fieldSid);
                detail.setSid(null);
                detail.insertOrUpdate();
            }
        }
    }

    @Override
    @Transactional
    public void doUpdate(SysField itemSysField) {

        itemSysField.insertOrUpdate();
        updateFieldDetail(itemSysField);
    }

    @Override
    @Transactional
    public void doDelete(String[] ids) {

        QueryWrapper<SysField> ew = new QueryWrapper<>();
        ew.in("sid", ids);
        new SysField().delete(ew);
    }

    @Override
    public SysField getOne(String sid) {

        //关联查询

        SysField itemSysField = getById(sid);

        return itemSysField;
    }

    @Override
    public List<SysField> all(SysField itemSysField) {

        return this.baseMapper.all(itemSysField);
    }


        @Override
    public Page<SysField> pageInfo(Page page, SysField itemSysField) {

        List<SysField> dataList = this.baseMapper.pageInfo(page, itemSysField);
        if(page ==null ){
            page = new Page(0,10000);
        }
        page.setRecords(dataList);
        return page;
    }


    @Override
    @Transactional
    public void doImport(List<SysField> dataList) {

        //数据验证

        //保存
        this.saveOrUpdateBatch(dataList);
    }

}
